package com.itheima.mapper;

import com.itheima.pojo.Category;
import com.itheima.pojo.Product;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ProductMapper {
    /*
     *   实现分页查询商品
     *
     * */
    List<Product> list(String name, Integer category_id, LocalDate begin, LocalDate end);

    /*
     *   更新商品
     * */
    void updateProduct(Product product);

    /*
     *   根据ids批量删除商品
     * */
    void deleteProductByIds(List<Integer> ids);

    /*
     *   新增商品
     * */
    @Insert("insert into products(name,image_url,description,unit,price,stock,category_id,create_time,update_time)" +
            "values(#{name},#{imageUrl},#{description},#{unit},#{price},#{stock},#{categoryId},#{createTime},#{createTime})")
    void addProduct(Product product);

    /*
     *   根据id查询商品
     * */
    @Select("select * from products where id = #{id}")
    Product getProduct(int id);

    /*
     *   查询所有商品分类
     * */
    @Select("select categories.*,count(products.id) as product_count from categories left join products on categories.id = products.category_id group by categories.id")
    List<Category> getcategoriesList(Integer page, Integer pageSize, String name);


    /*
     *    更新商品分类
     * */
    void updateCategory(Category category);

    /*
     *   根据id删除商品分类
     * */
    void deleteCategoryById(List<Integer> ids);

    /*
     *   新增商品分类
     * */
    @Insert("insert into categories(name,create_time,update_time) " + "values(#{name},#{createTime},#{updateTime})")
    void addCategory(Category category);

    /*
     *   根据id查询商品分类
     * */
    @Select("select * from categories where id = #{id}")
    Category selectCategoryById(int id);

    /*
     *   校验农产品名称
     * */
    @Select("select * from products where name = #{name}")
    Product validate(String name);

//    原始分页查询写法
//    @Select("select count(*) from products")
//    Long count();
//    @Select("select * from products limit #{start},#{pageSize}")
//    List<Product> page(Integer start,Integer pageSize);
//    @Select("select * from products")
}
